package com.sengled.cloud.biz.union.mgt.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.sengled.cloud.biz.union.mgt.common.util.CurrentUserUtil;
import com.sengled.cloud.biz.union.mgt.model.po.Shop;
import com.sengled.cloud.biz.union.mgt.model.request.SaveShopRequest;
import com.sengled.cloud.biz.union.mgt.model.response.ResponseMessage;
import com.sengled.cloud.biz.union.mgt.model.vo.SelectBoxDataVo;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopDetailVo;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopEditVo;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopQueryParamsBean;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopVo;
import com.sengled.cloud.biz.union.mgt.service.sys.impl.ShopService;

import io.swagger.annotations.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by FMC on 2017/10/27.
 */
@RestController
@RequestMapping("/api/v1")
public class ShopController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ShopService shopService;

    @ApiOperation(value = "获取店铺列表", notes = "获取店铺列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "shopCode", value = "店铺ID", dataType = "String", paramType = "query", defaultValue = "CY0001"),
            @ApiImplicitParam(name = "shopName", value = "店铺名称", dataType = "String", paramType = "query", defaultValue = "朝阳1店"),
            @ApiImplicitParam(name = "shopNature", value = "店铺属性", dataType = "Long", paramType = "query",defaultValue ="0"),
            @ApiImplicitParam(name = "pageNo", value = "当前页码", dataType = "Long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "显示条数", dataType = "Long", paramType = "query", defaultValue = "30")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    @RequestMapping(value = "/shops/", method = RequestMethod.GET)
    public ResponseMessage<List<ShopVo>> getShops(@ApiParam(name = "shopCode", required = true) String shopCode,
                                                      @ApiParam(name = "shopName", required = true) String shopName,
                                                      @ApiParam(name = "shopNature", required = true) Integer shopNature,
                                                      @ApiParam(name = "pageNo") Integer pageNo,
                                                      @ApiParam(name = "pageSize") Integer pageSize,HttpServletRequest request) {
        ShopQueryParamsBean query = new ShopQueryParamsBean();
        query.setPageNo(pageNo);
        query.setPageSize(pageSize);
        query.setShopCode(shopCode);
        query.setShopName(shopName);
        query.setShopNature(shopNature);

        Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        Long companyId = shopService.getCompanyId(customerId);
        query.setCompanyId(companyId);
        query.setUserId(customerId);
        
        return shopService.listShopsByPage(query);
    }

    @ApiOperation(value = "查看店铺", notes = "查看店铺", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "id", value = "店铺id", dataType = "Long", paramType = "path", required = true)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    
    @RequestMapping(value = "/shop/{id}", method = RequestMethod.GET)
    public ResponseMessage<ShopDetailVo> getById(@ApiParam(name = "id", required = true) @PathVariable Long id) {
        return shopService.getShopDetailById(id);
    }
    
    @ApiOperation(value = "编辑店铺查询", notes = "编辑店铺查询", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/shop/edit/{id}", method = RequestMethod.GET)
    public ResponseMessage<ShopEditVo> getEditPageById(@ApiParam(name = "id", required = true) @PathVariable Long id) {
        return shopService.getShopEditDataById(id);
    }

    @ApiOperation(value = "新增店铺保存", notes = "新增店铺保存接口", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    @RequestMapping(value = "/shop/", method = RequestMethod.POST)
    public ResponseMessage<Shop> createShop(@RequestBody SaveShopRequest saveShopRequest,HttpServletRequest request) {
    	logger.info("createShop request message is : " + saveShopRequest);
    	saveShopRequest.setId(null);
    	Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        return shopService.saveOrUpdate(saveShopRequest,customerId);
    }

    @ApiOperation(value = "编辑店铺保存", notes = "编辑店铺保存", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    @RequestMapping(value = "/shop/{id}", method = RequestMethod.PUT)
    public ResponseMessage<Shop> putMessage(@ApiParam(value = "店铺ID", required = true) @PathVariable Long id, @RequestBody SaveShopRequest saveShopRequest,HttpServletRequest request) {
    	saveShopRequest.setId(id);
    	Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        return shopService.saveOrUpdate(saveShopRequest,customerId);
    }


    @ApiOperation(value = "删除店铺", notes = "删除店铺", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误"),
            @ApiResponse(code = 20003, message = "执行失败")
    })
    @RequestMapping(value = "/shop/{id}", method = RequestMethod.DELETE)
    public ResponseMessage deleteMessage(@ApiParam(value = "店铺ID", required = true) @PathVariable Long id) {
        return shopService.delete(id);
    }

   
//    @ApiOperation(value = "上传消息接收客户名单upload请求", notes = "上传消息接收客户名单", produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiResponses(value = {
//            @ApiResponse(code = 200, message = "正常"),
//            @ApiResponse(code = 10001, message = "系统错误"),
//            @ApiResponse(code = 20002, message = "参数错误")
//    })
//    @RequestMapping(value = "/message/uploadExcel", method = RequestMethod.POST)
//    public String uploadExcel(@RequestParam("sourceFile") MultipartFile sourceFile) throws IOException {
//        //判断文件是否为空
//        if (sourceFile == null) {
//            return null;
//        }
//        //获取文件名
//        String name = sourceFile.getOriginalFilename();
//        //进一步判断文件是否为空（即判断其大小是否为0或其名称是否为null）
//        long size = sourceFile.getSize();
//        if (name == null || ("").equals(name) && size == 0) {
//            return null;
//        }
//
//        //批量导入。参数：文件名，文件。
//        List<MessageView> receivers = null;
//        JSONObject jsonObject = new JSONObject();
//        if (receivers != null && receivers.size() > 0) {
//            jsonObject.put("flag", 0);
//            jsonObject.put("success", "批量导入EXCEL成功");
//            JSONArray jsonArray = JSONArray.fromObject(receivers);
//            jsonObject.put("receivers", jsonArray);
//        } else {
//            jsonObject.put("flag", 1);
//            jsonObject.put("success", "批量导入EXCEL失败");
//        }
//        return jsonObject.toString();
//    }

}
